package com.hexacta.hrs.test.data;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.vectrics.system.PagePrivilege;
import org.vectrics.system.PagePrivilegeUserPrivilege;
import org.vectrics.user.UserPrivilegeType;

/**
 * Test data dependency creator for the PagePrivilege class.
 * 
 * @author tpollak
 */
public class PagePrivilegeTestDataDependencyCreator extends AbstractTestDataDependencyCreator {

    public PagePrivilegeTestDataDependencyCreator(List<TestDataDependencyCreator> dependencies) {
        super(dependencies);
    }

    public void createDependentTestData(Map<String, Object> context) {
        // Create PagePrivilege
        UserPrivilegeType adminAccess = this.loadUserPrivilege("admin.access");
        UserPrivilegeType companyView = this.loadUserPrivilege("company.view");
        UserPrivilegeType candidateProfileAccess = this.loadUserPrivilege("candidate.profile.access");
        this.createAndInsertPagePrivilege(context, 3000L, "/hrs-intranet/admin", "Administration Pages", Arrays.asList(
                adminAccess, companyView));
        this.createAndInsertPagePrivilege(context, 4000L, "/hrs-public/profile", "Public Site - Candidate Info", Arrays
                .asList(adminAccess, candidateProfileAccess));
    }

    private void addUserPrivileges(PagePrivilege privilege, Collection<UserPrivilegeType> userPrivileges) {
        long ppupId = privilege.getId();
        for (UserPrivilegeType userPrivilegeType : userPrivileges) {
            PagePrivilegeUserPrivilege ppup = new PagePrivilegeUserPrivilege();
            ppup.setId(ppupId);
            ppup.setUserPrivilegeType(userPrivilegeType);
            ppup.setPagePrivilege(privilege);
            ppup = this.insertDomainObject(ppup);
            privilege.getPagePrivilegeUserPrivileges().add(ppup);
            ppupId++;
        }
    }

    private PagePrivilege createAndInsertPagePrivilege(Map<String, Object> context, Long id, String page,
            String description, Collection<UserPrivilegeType> userPrivileges) {
        PagePrivilege privilege = this.createPagePrivilege(id, page, description, userPrivileges);
        privilege = this.insertDomainObject(privilege);
        this.addUserPrivileges(privilege, userPrivileges);
        return privilege;
    }

    private PagePrivilege createPagePrivilege(Long id, String page, String description,
            Collection<UserPrivilegeType> userPrivileges) {
        PagePrivilege privilege = new PagePrivilege();
        privilege.setId(id);
        privilege.setPage(page);
        privilege.setDescription(description);
        privilege.setPagePrivilegeUserPrivileges(new HashSet<PagePrivilegeUserPrivilege>());

        return privilege;
    }

    private UserPrivilegeType loadUserPrivilege(String privilegeCode) {
        UserPrivilegeType privilege = (UserPrivilegeType) this.getCurrentSession().load(UserPrivilegeType.class,
                privilegeCode);
        return privilege;
    }
}
